//
//  LCAudioViewController.m
//  TheLCNews
//
//  Created by apple on 15/11/26.
//  Copyright © 2015年 LC. All rights reserved.
//

#import "LCAudioViewController.h"
#import "LCAudioCoCell.h"
#import "LCAudioTbCell.h"
#import "LCAudelModel.h"
#import "LCPlayViewController.h"
#import "Lchao.h"
#define kWIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define kHEIGHT ([UIScreen mainScreen].bounds.size.height/667)

@interface LCAudioViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate,MBProgressHUDDelegate>
@property (nonatomic, strong) UIButton *recommendbutton;

@property (nonatomic, strong) UIButton *hotbutton;

@property (nonatomic, strong) UICollectionView *recommendCollectionView;

@property (nonatomic, strong) UICollectionView *hotCollectionView;

@property (nonatomic, strong) UITableView *hotTableView;

@property (nonatomic, strong) NSMutableArray *recommendDataArr;

@property (nonatomic, strong) NSMutableArray *recommendMainArr;

@property (nonatomic, strong) NSMutableArray *hotDataArr1;

@property (nonatomic, strong) NSMutableArray *hotMainArr1;

@property (nonatomic, strong) NSMutableArray *hotDataArr2;

@property (nonatomic, strong) NSMutableArray *hotMainArr2;

@property (nonatomic, strong) NSMutableArray *hotDataArr3;

@property (nonatomic, strong) NSMutableArray *hotMainArr3;

@property (nonatomic, assign) NSInteger *hotPage;

@property (nonatomic, assign) NSInteger *recommendPage;

@property (nonatomic, assign) NSInteger *hotMaxPage;

@property (nonatomic, assign) NSInteger *recommendMaxPage;

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong)NSArray *tempArray;
@property (nonatomic, strong)LCCoreDateManager *manager;

@property (nonatomic, strong) UIView *titleView;
@property (nonatomic, strong) MBProgressHUD *hud;
@end

@implementation LCAudioViewController

-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self) {
    }
    return self;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor grayColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.manager = [LCCoreDateManager defaults];
    
    self.recommendDataArr = [NSMutableArray array];
    self.recommendMainArr = [NSMutableArray array];
    
    self.hotDataArr1 = [NSMutableArray array];
    self.hotMainArr1 = [NSMutableArray array];
    
    self.hotDataArr2 = [NSMutableArray array];
    self.hotMainArr2 = [NSMutableArray array];
    
    self.hotDataArr3 = [NSMutableArray array];
    self.hotMainArr3 = [NSMutableArray array];
    
    self.recommendPage = 0;
    self.hotPage = 0;


     //创建标题背景视图
    self.titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH , 64)];
    self.titleView.backgroundColor = [UIColor greenColor];
     [self.view addSubview:self.titleView];
    
    
    //视图切换button
    [self createButton];
    //scrollView
    [self createScrollView];
    
    
    //热推荐数据
    [self commendDataHandle];
    //热门数据
    [self hotDataHandle];
    //推荐页面
    [self createCommendCollectionView];
    //热门页面
    [self createHotTableView];
    
    [self createHotCollectionView];
    
    }

- (void)createButton
{
    self.recommendbutton = [UIButton buttonWithType:UIButtonTypeCustom];

    self.recommendbutton.frame = CGRectMake(kWIDTH * 145, kHEIGHT * 20, kWIDTH * 40, kHEIGHT * 20);
    [self.titleView addSubview:self.recommendbutton];
    
    [self.recommendbutton setTitle:@"推荐" forState:UIControlStateNormal];
    [self.recommendbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.recommendbutton.titleLabel.font = [UIFont systemFontOfSize:15 * kWIDTH];
    [self.recommendbutton addTarget:self action:@selector(recommendbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    self.hotbutton = [UIButton buttonWithType:UIButtonTypeCustom];

    [self.titleView addSubview:self.hotbutton];
    self.hotbutton.frame = CGRectMake(kWIDTH * 190, kHEIGHT * 20, kWIDTH * 40, kHEIGHT * 20);
    
    [self.hotbutton setTitle:@"热门" forState:UIControlStateNormal];
    [self.hotbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    self.hotbutton.titleLabel.font = [UIFont systemFontOfSize:15 * kWIDTH];
    [self.hotbutton addTarget:self action:@selector(hotbuttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)createScrollView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, kWIDTH * 375, kHEIGHT * 667 -64)];
    
    self.scrollView.backgroundColor = [UIColor grayColor];
    
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    
    [self.view addSubview:self.scrollView];
    
    /* 整页滚动 */
    self.scrollView.pagingEnabled = YES;
    
    /* 视图左上角距离原点的偏移量 */
    self.scrollView.contentOffset = CGPointMake(0, 0);
    
    /* 是否显示垂直滚动条, 默认值为YES */
    self.scrollView.showsVerticalScrollIndicator = NO;
    
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    /* 边界是否回弹 */
    self.scrollView.bounces = NO;
    
    self.scrollView.delegate = self;
    
   
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //判断如果是大的scollView时
    if ([[NSString stringWithFormat:@"%@",[scrollView class]] isEqualToString:@"UIScrollView"]){
        
        if (scrollView.contentOffset.x == WIDTH ) {
            
            [self.recommendbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [self.hotbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            
        }else if(scrollView.contentOffset.x == 0) {
            
            [self.recommendbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.hotbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }
}

- (void)recommendbuttonClicked:(UIButton *)button
{
    [self.recommendbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.hotbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    self.scrollView.contentOffset = CGPointMake(0, 0);
}

-(void)hotbuttonClicked:(UIButton *)button
{
    [self.recommendbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.hotbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
}



- (void)commendDataHandle
{
//    self.hud = [[MBProgressHUD alloc] initWithView:self.recommendCollectionView];
//    [self.view addSubview:_hud];
//    [self.view bringSubviewToFront:_hud];
//    self.hud.delegate = self;
//    self.hud.labelText = @"加载中...";
//    [self.hud show:YES];

    [Network networkGETRequesWithURL:@"http://echosystem.kibey.com/sound/hot?page=1" Paramater:nil pageUniquenessIdentifier:@"http://echosystem.kibey.com/sound/hot?page=1" result:^(id result) {
        
        self.recommendDataArr = [[result objectForKey:@"result"] objectForKey:@"data"];
        
        
        self.recommendMainArr = [LCAudelModel baseModelByArr:self.recommendDataArr];
        
        
        
        if (self.recommendMainArr.count > 0) {
            
            [self.recommendCollectionView reloadData];
        }
    }];
//    [self.hud removeFromSuperview];
}

- (void)hotDataHandle
{
    NSString *url = @"http://echosystem.kibey.com/hot/sounds";
    
    [Network networkPOSTRequestWithURL:url body:@"v=9&period=daily" pageUniquenessIdentifier:@"v=9&period=daily" result:^(id result) {
        
        self.hotDataArr1 = [[result objectForKey:@"result"] objectForKey:@"data"];
        
        
        self.hotMainArr1 = [LCAudelModel baseModelByArr:self.hotDataArr1];
        
        
        
        if (self.hotMainArr1.count > 0) {
            
            [self.hotCollectionView reloadData];
        }
    }];
    
    [Network networkPOSTRequestWithURL:url body:@"v=9&period=weekly" pageUniquenessIdentifier:@"v=9&period=weekly" result:^(id result) {
        
        self.hotDataArr2 = [[result objectForKey:@"result"] objectForKey:@"data"];
        
        self.hotMainArr2 = [LCAudelModel baseModelByArr:self.hotDataArr2];
        if (self.hotMainArr2.count > 0) {
            
            [self.hotTableView reloadData];
            [self.hotCollectionView reloadData];
            
        }
        
    }];
    
    [Network networkPOSTRequestWithURL:url body:@"v=9&period=monthly" pageUniquenessIdentifier:@"v=9&period=monthly" result:^(id result) {
        
        self.hotDataArr3 = [[result objectForKey:@"result"] objectForKey:@"data"];
        
        self.hotMainArr3 = [LCAudelModel baseModelByArr:self.hotDataArr3];        if (self.hotMainArr3.count > 0) {
            
            [self.hotTableView reloadData];
            [self.hotCollectionView reloadData];
            
        }
        
    }];
    
}


- (void)createCommendCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.recommendCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH * 375, kHEIGHT * 667 - 64 ) collectionViewLayout:flowLayout];
    [self.scrollView addSubview:self.recommendCollectionView];
    
    self.recommendCollectionView.backgroundColor = [UIColor whiteColor];
    
    self.recommendCollectionView.delegate = self;
    self.recommendCollectionView.dataSource = self;
    
    self.recommendCollectionView.tag = 1000;
    
    //    [self.recommendCollectionView release];
    
    [self.recommendCollectionView registerClass:[LCAudioCoCell class] forCellWithReuseIdentifier:@"LCAudioCoCell1"];
    
    self.recommendCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(recommendHeaderFreshing)];
    
    self.recommendCollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(recommendFooterFreshing)];
    
}

- (void)recommendHeaderFreshing
{
    
    [self commendDataHandle];
    
    [self.recommendCollectionView.mj_header endRefreshing];
}


- (void)recommendFooterFreshing
{
    self.recommendPage++;
    
    NSString *url = [NSString stringWithFormat:@"http://echosystem.kibey.com/sound/hot?page=%ld",self.recommendPage];
    
    [Network networkGETRequesWithURL:url Paramater:nil pageUniquenessIdentifier:url result:^(id result) {
        
        self.recommendDataArr = [[result objectForKey:@"result"] objectForKey:@"data"];
        
             NSArray *arr = [LCAudelModel baseModelByArr:self.recommendDataArr];
        
        [self.recommendMainArr addObjectsFromArray:arr];
        
         if (self.recommendMainArr.count > 0) {
            
            [self.recommendCollectionView reloadData];
        }
        
    }];
    
    [self.recommendCollectionView.mj_footer endRefreshing];
    
    if (self.recommendMaxPage) {
        NSLog(@"%ld, %ld", (long)self.recommendPage, (long)self.recommendMaxPage);
        self.recommendCollectionView.mj_footer.hidden = YES;
    }
}


- (void)createHotTableView
{
    self.hotTableView = [[UITableView alloc] initWithFrame:CGRectMake(kWIDTH * 375, 0, kWIDTH * 375, kHEIGHT * 667 - kHEIGHT * 64 - 20) style:UITableViewStyleGrouped];
    
    [self.scrollView addSubview:self.hotTableView];
    
    self.hotTableView.backgroundColor = [UIColor whiteColor];
    
    self.hotTableView.delegate = self;
    self.hotTableView.dataSource = self;
    
    self.hotTableView.rowHeight = kHEIGHT * 80;
    
    self.hotTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(hotHeaderFreshing)];
    
    
}


- (void)hotHeaderFreshing
{
        [self hotDataHandle];
    
    [self.hotTableView.mj_header endRefreshing];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        return 50 * kHEIGHT;
    }else{
        
        return kHEIGHT * 40;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        
        if (self.hotMainArr2.count > 0) {
            
            return self.hotMainArr2.count;
            
        }else{
            
            return 0;
        }
        
    }else{
        
        if (self.hotMainArr3.count > 0) {
            
            return self.hotMainArr3.count;
            
        }else{
            
            return 0;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCAudioTbCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LCAudioTbCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell) {
        cell = [[LCAudioTbCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LCAudioTbCell"];
    }
    
    if (indexPath.section == 0) {
        
        if (self.hotMainArr2.count > 0) {
            cell.model = [self.hotMainArr2 objectAtIndex:indexPath.row];
            
        }
        
    }else{
        
        if (self.hotMainArr3.count > 0) {
            cell.model = [self.hotMainArr3 objectAtIndex:indexPath.row];
        }
        
    }
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"本周热门";
    }else{
        
        return @"本月热门";
    }
}

- (void)createHotCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    
    self.hotCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWIDTH * 375, kHEIGHT * 220 * 3) collectionViewLayout:flowLayout];
    
    self.hotTableView.tableHeaderView = self.hotCollectionView;
    self.hotCollectionView.backgroundColor = [UIColor whiteColor];
    
    self.hotCollectionView.delegate = self;
    self.hotCollectionView.dataSource = self;
    
    self.hotCollectionView.scrollEnabled = NO;
    
    self.hotCollectionView.tag = 2000;
    
    
    [self.hotCollectionView registerClass:[LCAudioCoCell class] forCellWithReuseIdentifier:@"LCAudioCoCell2"];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kWIDTH * 10, kHEIGHT * 15, kWIDTH * 375, kHEIGHT * 30)];
    [self.hotCollectionView addSubview:titleLabel];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.font = [UIFont systemFontOfSize:13.5];
    titleLabel.text = @"今日最热";
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        LCPlayViewController *playController = [LCPlayViewController sharePlayer];
        
        [[LCPlayViewController sharePlayer] setData:self.hotMainArr2 row:indexPath.row];
        LCAudelModel *model = [self.hotMainArr2 objectAtIndex:indexPath.row];
        //传参数
        
        playController.name = model.name;
        playController.pic = model.pic;
        playController.playUrl = model.source;
        playController.indexRow = indexPath.row;
        
                [self searchHistory];
                int count = 0;
                if (self.tempArray.count == 0) {
                    Lchao *list = [NSEntityDescription insertNewObjectForEntityForName:@"Lchao" inManagedObjectContext:self.manager.managedObjectContext];
                    list.name = model.name;
                    list.pic = model.pic;
                    list.source = model.source;
                    [self.manager saveContext];
    }
    else{
                    for (Lchao *modelll in self.tempArray) {
                        if (![modelll.source isEqualToString:model.source]) {
                            count++;
                        }
                    }
                    if (count == self.tempArray.count) {
                        Lchao *list = [NSEntityDescription insertNewObjectForEntityForName:@"Lchao" inManagedObjectContext:self.manager.managedObjectContext];
                        list.name = model.name;
                        list.pic = model.pic;
                        list.source = model.source;
        
                        [self.manager saveContext];
                    }
                }
        
                [self.navigationController pushViewController:playController animated:YES];
        
            }else{
        LCPlayViewController *playController = [LCPlayViewController sharePlayer];
        
        [[LCPlayViewController sharePlayer] setData:self.hotMainArr3 row:indexPath.row];
        LCAudelModel *model = [self.hotMainArr3 objectAtIndex:indexPath.row];
        
        //传参数
        playController.name = model.name;
        playController.pic = model.pic;
        playController.playUrl = model.source;
        playController.indexRow = indexPath.row;
                [self searchHistory];
                int count = 0;
                if (self.tempArray.count == 0) {
                    Lchao *list = [NSEntityDescription insertNewObjectForEntityForName:@"Lchao" inManagedObjectContext:self.manager.managedObjectContext];
                    list.name = model.name;
                    list.pic = model.pic;
                    list.source = model.source;
                    [self.manager saveContext];
        
        [self.navigationController pushViewController:playController animated:YES];
    }
            else{
                for (Lchao *modelll in self.tempArray) {
                    if (![modelll.source isEqualToString:model.source]) {
                        count++;
                    }
                }
                if (count == self.tempArray.count) {
                    Lchao *list = [NSEntityDescription insertNewObjectForEntityForName:@"Lchao" inManagedObjectContext:self.manager.managedObjectContext];
                    list.name = model.name;
                    list.pic = model.pic;
                    list.source = model.source;
    
                    [self.manager saveContext];
                }
            }
    
    
            [self.navigationController pushViewController:playController animated:YES];
        }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (collectionView.tag == 1000) {
        return 1;
    }else{
        return 1;
    }
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView.tag == 1000) {
        
        if (self.recommendMainArr.count > 0) {
            return self.recommendMainArr.count;
        }else{
            return 0;
        }
        
    }else{
        
        if (self.hotMainArr1.count > 0) {
            return self.hotMainArr1.count;
        }else{
            return 0;
        }
        
    }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (collectionView.tag == 1000) {
        
        LCAudioCoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCAudioCoCell1" forIndexPath:indexPath];
        
        if (self.recommendMainArr.count > 0) {
            cell.model = [self.recommendMainArr objectAtIndex:indexPath.item];
        }
        
        return cell;
    }else{
        
        LCAudioCoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCAudioCoCell2" forIndexPath:indexPath];
        
        if (self.hotMainArr1.count > 0) {
            
            cell.model = [self.hotMainArr1 objectAtIndex:indexPath.item];
            
        }
        
        return cell;
    }
}

//item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((kWIDTH * 375 - kWIDTH * 30) / 2, kHEIGHT * 220);
}
//item距四周的距离
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(kHEIGHT * 10, kWIDTH * 8, kHEIGHT * 10, kWIDTH * 8);
}
//头视图的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (collectionView.tag == 2000) {
        return CGSizeMake(kWIDTH * 375, kHEIGHT * 40);
    }else{
        
        return CGSizeMake(0, kHEIGHT * 20);
    }
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView.tag == 1000) {
        LCPlayViewController *playController = [LCPlayViewController sharePlayer];
        
        [[LCPlayViewController sharePlayer] setData:self.recommendMainArr row:indexPath.row];
        LCAudelModel *model = [self.recommendMainArr objectAtIndex:indexPath.row];
        
        //传参数
        playController.name = model.name;
        playController.pic = model.pic;
        playController.playUrl = model.source;
        playController.indexRow = indexPath.row;
                [self searchHistory];
                int count = 0;
                if (self.tempArray.count == 0) {
                    Lchao *list = [NSEntityDescription insertNewObjectForEntityForName:@"Lchao" inManagedObjectContext:self.manager.managedObjectContext];
                    list.name = model.name;
                    list.pic = model.pic;
                    list.source = model.source;
                    [self.manager saveContext];
                }
                else{
                    for (Lchao *modelll in self.tempArray) {
                        if (![modelll.source isEqualToString:model.source]) {
                            count++;
                        }
                    }
                    if (count == self.tempArray.count) {
                        Lchao *list = [NSEntityDescription insertNewObjectForEntityForName:@"Lchao" inManagedObjectContext:self.manager.managedObjectContext];
                        list.name = model.name;
                        list.pic = model.pic;
                        list.source = model.source;
        
                        [self.manager saveContext];
                    }
                }
        
        [self.navigationController pushViewController:playController animated:YES];
    }if (collectionView.tag == 2000) {
        LCPlayViewController *playController = [LCPlayViewController sharePlayer];
        
        [[LCPlayViewController sharePlayer] setData:self.hotMainArr1 row:indexPath.row];
        LCAudelModel *model = [self.hotMainArr1 objectAtIndex:indexPath.row];
        //传参数
        playController.name = model.name;
        playController.pic = model.pic;
        playController.playUrl = model.source;
        playController.indexRow = indexPath.row;
                [self searchHistory];
                int count = 0;
                if (self.tempArray.count == 0) {
                    Lchao *list = [NSEntityDescription insertNewObjectForEntityForName:@"Lchao" inManagedObjectContext:self.manager.managedObjectContext];
                    list.name = model.name;
                    list.pic = model.pic;
                    list.source = model.source;
                    [self.manager saveContext];
    }
            else{
                for (Lchao *modelll in self.tempArray) {
                    if (![modelll.source isEqualToString:model.source]) {
                        count++;
                    }
                }
                if (count == self.tempArray.count) {
                    Lchao *list = [NSEntityDescription insertNewObjectForEntityForName:@"Lchao" inManagedObjectContext:self.manager.managedObjectContext];
                    list.name = model.name;
                    list.pic = model.pic;
                    list.source = model.source;
    
                    [self.manager saveContext];
                }
            }
    
            [self.navigationController pushViewController:playController animated:YES];
    
        }
}
-(void)searchHistory{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Lchao" inManagedObjectContext:self.manager.managedObjectContext];
    [fetchRequest setEntity:entity];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"source"
                                                                   ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    NSError *error = nil;
    NSArray *fetchedObjects = [self.manager.managedObjectContext executeFetchRequest:fetchRequest error:&error];
    self.tempArray = fetchedObjects;
    NSLog(@"%@", self.tempArray);
    if (fetchedObjects == nil) {
        
    }else{
        
    }
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
